/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 *  
 */
public class AccountMetaModel extends OrderMetaModel
{
	protected ProductMetaModel cartMetaModel;
	
	protected int addressBookEntriesMax;

	protected boolean displayDOB;
	protected boolean displayGender;

	protected int entryCityLengthMin;
	protected int entryDobLengthMin;
	protected int entryEmailAddressLengthMin;
	protected boolean entryEmailAddressFormatCheck;
	protected int entryFirstnameLengthMin;
	protected int entryLastnameLengthMin;
	protected int entryPasswordLengthMin;
	protected int entryPostcodeLengthMin;
	protected int entryStateLengthMin;
	protected int entryStreetAddressLengthMin;
	protected int entryTelephoneLengthMin;
	protected String adminUrl;
	protected boolean requireAdministrator;

	protected List<CountryModel> countryModels;

	public AccountMetaModel(JSONObject jsonObject)
	{
		super(jsonObject);
		
		countryModels = new ArrayList<CountryModel>();

		displayDOB = convertToBoolean(jsonObject, "display_dob");
		displayGender = convertToBoolean(jsonObject, "display_gender");

		entryCityLengthMin = convertToInteger(jsonObject, "entry_city_length_min");
		entryDobLengthMin = convertToInteger(jsonObject, "entry_dob_length_min");
		entryEmailAddressLengthMin = convertToInteger(jsonObject, "entry_email_address_length_min");
		entryEmailAddressFormatCheck = convertToBoolean(jsonObject, "entry_email_address_format_check");
		entryFirstnameLengthMin = convertToInteger(jsonObject, "entry_firstname_length_min");
		entryLastnameLengthMin = convertToInteger(jsonObject, "entry_lastname_length_min");
		entryPasswordLengthMin = convertToInteger(jsonObject, "entry_password_length_min");
		entryPostcodeLengthMin = convertToInteger(jsonObject, "entry_postcode_length_min");
		entryStateLengthMin = convertToInteger(jsonObject, "entry_state_length_min");
		entryStreetAddressLengthMin = convertToInteger(jsonObject, "entry_street_address_length_min");
		entryTelephoneLengthMin = convertToInteger(jsonObject, "entry_telephone_length_min");
		addressBookEntriesMax = convertToInteger(jsonObject, "address_book_entries_max");
		adminUrl = convertToString(jsonObject, "admin_url");
		requireAdministrator = convertToBoolean(jsonObject, "require_administrator");

		buildCountries(jsonObject);
		buildZones(jsonObject);
		buildCart(jsonObject);
	}

	protected void buildCountries(JSONObject jsonObject)
	{
		countryModels = new ArrayList<CountryModel>();

		try {
			JSONValue countryValue = jsonObject.get("countries");

			if (countryValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = countryValue.isArray()) != null)
				{
					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonCObject;
						if ((jsonCObject = jsonValue.isObject()) != null)
						{
							CountryModel countryModel = new CountryModel(jsonCObject);
							countryModels.add(countryModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}

	protected void buildZones(JSONObject jsonObject)
	{
		try {
			JSONValue countryValue = jsonObject.get("zones");

			if (countryValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = countryValue.isArray()) != null)
				{
					CountryModel countryModel = countryModels.get(0);
					
					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonCObject;
						if ((jsonCObject = jsonValue.isObject()) != null)
						{
							ZoneModel zone = new ZoneModel(jsonCObject);
							String countryName = zone.getCountryName();
							if (countryModel.getName().equals(countryName))
							{
								countryModel.addZone(zone);
							}
							else
							{
								int sz1 = countryModels.size();
								for (int j = 0; j < sz1; j++)
								{
									countryModel = countryModels.get(j);
									if (countryModel.getName().equals(countryName))
									{
										countryModel.addZone(zone);
										break;
									}
								}
							}
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}

	protected void buildCart(JSONObject jsonObject)
	{
		try {
			JSONValue cmdValue = jsonObject.get("cart_meta_data");

			if (cmdValue != null)
			{
				JSONObject jsonCMMObject;
				if ((jsonCMMObject = cmdValue.isObject()) != null)
				{
					cartMetaModel = new ProductMetaModel(jsonCMMObject);
				}
			}
		}
		catch (Exception e) {GWT.log("AccountMetaModel.buildCart", e);}
	}

	public int getAddressBookEntriesMax() {
		return addressBookEntriesMax;
	}

	public int getEntryCityLengthMin() {
		return entryCityLengthMin;
	}

	public int getEntryDobLengthMin() {
		return entryDobLengthMin;
	}

	public int getEntryEmailAddressLengthMin() {
		return entryEmailAddressLengthMin;
	}

	public int getEntryFirstnameLengthMin() {
		return entryFirstnameLengthMin;
	}

	public int getEntryLastnameLengthMin() {
		return entryLastnameLengthMin;
	}

	public int getEntryPasswordLengthMin() {
		return entryPasswordLengthMin;
	}

	public int getEntryPostCodeLengthMin() {
		return entryPostcodeLengthMin;
	}

	public int getEntryStateLengthMin() {
		return entryStateLengthMin;
	}

	public int getEntryStreetAddressLengthMin() {
		return entryStreetAddressLengthMin;
	}

	public int getEntryTelephoneNumberLengthMin() {
		return entryTelephoneLengthMin;
	}

	public boolean isDisplayDOB() {
		return displayDOB;
	}

	public boolean isDisplayGender() {
		return displayGender;
	}

	public List<CountryModel> getCountryModels() {
		return countryModels;
	}

	public void setCountryModels(List<CountryModel> countryModels) {
		this.countryModels = countryModels;
	}

	public ProductMetaModel getCartMetaModel() {
		return cartMetaModel;
	}

	public boolean isEntryEmailAddressFormatCheck() {
		return entryEmailAddressFormatCheck;
	}
	
	public String getAdminUrl() {
		return adminUrl;
	}

	public boolean isRequireAdministrator() {
		return requireAdministrator;
	}

	public void setRequireAdministrator(boolean requireAdministrator) {
		this.requireAdministrator = requireAdministrator;
	}
}
